﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.Util;
using Emgu.CV.UI;
using System.Diagnostics;

namespace CCD
{
    public partial class BlockDefine : Form
    {
        /// <summary>
        /// GLOBAL
        /// </summary>
        public Image<Gray, Byte> imgMask;
        private Image<Rgb, Byte> imgOrg;
//        public Rectangle rect;


        /// <summary>
        /// TEMP FILE INFO
        /// </summary>
        string _name = "";
        string _blockPath = "";
        string _maskFileName = "";
        string _tempFileName = "temp\\blockDefine.bmp";

        Image<Gray, Byte> _imgMask;


        #region CONSTRUCTOR

        public BlockDefine()
        {
            InitializeComponent();
        }

        public BlockDefine(string blockName, string blockPath, Image<Rgb, Byte> img)
        {
            _name = blockName;
            _blockPath = blockPath;
            _maskFileName = Path.Combine(blockPath, "mask.bmp");

            imgOrg = img.Clone();

            InitializeComponent();

            this.Text += (" —— " + blockName);
        }

        public BlockDefine(string blockName, string fileName, string imgBack)
        {
            _name = blockName;
            //            _imgMask = returnImg;
            _blockPath = fileName;

            imgOrg = new Image<Rgb, byte>(imgBack);

            InitializeComponent();

            this.Text += (" —— " + blockName);
        }

        #endregion

        public Image<Gray, Byte> GetBlockMask()
        {
            Image<Rgb, Byte> img2 = new Image<Rgb, Byte>(_tempFileName);

            imgMask = img2.AbsDiff(imgOrg).Convert<Gray, Byte>().ThresholdBinary(new Gray(100), new Gray(255));

            imageBox1.Image = imgMask;
            
            return imgMask;
        }

        private void button1_Click(object sender, EventArgs e)
        {                      

            imgOrg = imgOrg.Mul(0.5);
            imgOrg.Save(_tempFileName);

            Process msPaint = new Process();
            msPaint.StartInfo.FileName = "mspaint.exe";
            msPaint.StartInfo.Arguments = _tempFileName;
            msPaint.Start();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            // 蒙版
            _imgMask = GetBlockMask().Clone();
            // 保存蒙版          
            _imgMask.Save(_maskFileName);
            // 获取ROI
            int top = _imgMask.Height;
            int bottom = 0;
            int left = _imgMask.Width;
            int right = 0;
            for ( int i = 0; i < _imgMask.Height; i ++ )
                for ( int j = 0; j < _imgMask.Width; j ++ )
                    if (_imgMask[i, j].Intensity > 100)
                    {
                        if (top > i) top = i;
                        if (bottom < i) bottom = i;
                        if (left > j) left = j;
                        if (right < j) right = j;
                    }
            Rectangle rect = new Rectangle(left, top, right - left, bottom - top);
            // 保存 配置文件 config.ini
            FileStream fs = new FileStream(Path.Combine(_blockPath, "config.ini"), FileMode.Create, FileAccess.Write);
            StreamWriter wt = new StreamWriter(fs);
                // ROI
            wt.WriteLine("{0:d},{1:d},{2:d},{3:d}", new object[] { rect.X, rect.Y, rect.Width, rect.Height });
                // Precision
            wt.WriteLine("{0:g}", 0.5123423);

            wt.Close(); fs.Close();

        }

        private void BlockDefine_Load(object sender, EventArgs e)
        {

        }
    }
}
